<?php
session_start (); // ne recrée pas une session mais récupère la dernière (provenant de start.php)
$_SESSION ['pathway'] = '..';
include ($_SESSION ['pathway'] . "/header.php");
require_once ($_SESSION ['pathway'] . "/commun/ouvre_base.php");

/*
 * print_r($_POST);
 */

if (! isset ( $_SESSION ['id'] )) {
	echo "<table class='principale'><tr><td>Session expired</td></tr>";
	echo "<tr><td><a href='" . $_SESSION ['pathway'] . "/index.php'>Start</a></td></tr></table>";
} else {
	
	?><a href="<?php echo $_SESSION['pathway']; ?>/index.php">Home</a>
>
<a href='./choix.php'> Clinics </a><?php
	
	if (isset ( $_GET ['ns'] )) {
		$_SESSION ['delete_form'] = $_GET ['ns'];
		// print_r($_SESSION);
		require_once ("./requetes/no_save.php");
	}
	
	if (isset ( $_POST ['code1'] ) and $_POST ['code1'] != '' or isset ( $_POST ['code2'] ) and $_POST ['code2'] != '' or isset ( $_POST ['code3'] ) and $_POST ['code3'] != '' or isset ( $_POST ['code4'] ) and $_POST ['code4'] != '' or isset ( $_POST ['code5'] ) and $_POST ['code5'] != '') {
		
		$requete_form_numbers = "SELECT * FROM `diagnostic` WHERE ";
		$requete = '';
		if ($_POST ['code1'] != '') {
			$requete .= " AND `code1` LIKE '" . $_POST ['code1'] . "'";
		}
		if ($_POST ['code2'] != '') {
			$requete .= " AND `code2` LIKE '" . $_POST ['code2'] . "'";
		}
		if ($_POST ['code3'] != '') {
			$requete .= " AND `code3` LIKE '" . $_POST ['code3'] . "'";
		}
		if ($_POST ['code4'] != '') {
			$requete .= " AND `code4` LIKE '" . $_POST ['code4'] . "'";
		}
		if ($_POST ['code5'] != '') {
			$requete .= " AND `code5` LIKE '" . $_POST ['code5'] . "'";
		}
		$requete = ltrim ( $requete, " AND" );
		$requete_finale_form_numbers = $requete_form_numbers . $requete;
		// echo $requete_finale_form_numbers;
		$result_recup_entete = mysql_query ( $requete_finale_form_numbers ) or die ( "Impossible de recupérer les ID des forms : " . mysql_error () );
		$liste_IDs = Array ();
		$liste_individus = Array ();
		while ( $ligne = mysql_fetch_assoc ( $result_recup_entete ) ) {
			array_push ( $liste_IDs, $ligne ['form'] );
			array_push ( $liste_individus, $ligne ['code1'] . '-' . $ligne ['code2'] . '-' . $ligne ['code3'] . '-' . $ligne ['code4'] . '-' . $ligne ['code5'] );
		}
		echo "<table class='principale'>";
		$compteur = 0;
		foreach ( $liste_IDs as $form ) {
			echo "<tr><td>" . $liste_individus [$compteur] . "</td><td><a href=\"./page_principale.php?form=" . $form . "\"> Form number " . $form . "</a></td></tr>";
			$compteur ++;
		}
		echo "</table>";
	} else if (isset ( $_POST ['form'] ) and $_POST ['form'] != '') {
		$form = $_POST ['form'];
		$verifie_existence_form = "SELECT COUNT(*) > 0 FROM `$db`.`diagnostic` WHERE `form`=$form";
		// echo $verifie_existence_form;
		$resultat_verifie_existence = mysql_query ( $verifie_existence_form );
		$ligne = mysql_fetch_row ( $resultat_verifie_existence );
		$res = $ligne [0];
		if ($res == 1) {
			?>
<table class='principale'>
	<tr>
		<td><a
			href="./page_principale.php?form=<?php echo $form; ?>&amp;from=choix"> Form number <?php echo $form; ?> </a></td>
	</tr>
</table>
<?php
		} elseif ($res == 0) {
			?>
<table class='principale'>
	<tr>
		<td>Form <?php echo $form; ?> not found in database</td>
	</tr>
</table>
<?php
		}
	} else if (isset ( $_POST ['activite'] ) and $_POST ['activite'] != '') {
		echo "<table class='principale'>";
		
		$activite = $_POST ['activite'];
		$verifie_existence_form_activite = "SELECT COUNT(*) > 0 FROM `$db`.`form_activite` WHERE `nip`=$activite";
		$resultat_verifie_existence = mysql_query ( $verifie_existence_form_activite );
		$ligne = mysql_fetch_row ( $resultat_verifie_existence );
		$res_table_formactivite = $ligne [0];
		if ($res_table_formactivite == 1) {
			echo "<tr><td><a href=\"./see_form_activite.php?form=$activite\" target=_blank > Activity record $activite</a></td></tr>";
		}
		
		$verifie_existence_diagnostic = "SELECT COUNT(*) > 0 FROM `$db`.`diagnostic` WHERE `nip`=$activite";
		$resultat_verifie_existence = mysql_query ( $verifie_existence_diagnostic );
		$ligne = mysql_fetch_row ( $resultat_verifie_existence );
		$res_table_diagnostic = $ligne [0];
		if ($res_table_diagnostic == 1) {
			$recup_form_par_nip = "SELECT `form` FROM `$db`.`diagnostic` WHERE `nip`=$activite";
			$res_recupformparnip = mysql_query ( $recup_form_par_nip );
			while ( $ligne = mysql_fetch_row ( $res_recupformparnip ) ) {
				echo "<tr><td><a href=\"./page_principale.php?form=" . $ligne [0] . "\"> Form number " . $ligne [0] . "</a></td></tr>";
			}
		}
		
		?></table><?php
		
		if ($res_table_formactivite == 0 and $res_table_diagnostic == 0) {
			echo "<table class='principale'><tr><td>Couldn&apos;t find NIP number $activite in database</td></tr></table>";
		}
	} else {
		$form = '';
		
		?>
<table class='principale'>
	<tr>
		<form action="choix.php" method='post'>
			<td><b>Search :</b></td>
			<td>By ID :</td>
			<td><input type="text" maxlength="3" size="1" name="code1" id="code1"
				value="<?php if(isset($hash_champ_valeur['code1'])){echo $hash_champ_valeur['code1']; } ?>" />-
				<input type="text" maxlength="3" size="1" name="code2" id="code2"
				value="<?php if(isset($hash_champ_valeur['code2'])){echo $hash_champ_valeur['code2']; } ?>" />-
				<input type="text" maxlength="3" size="1" name="code3" id="code3"
				value="<?php if(isset($hash_champ_valeur['code3'])){echo $hash_champ_valeur['code3']; } ?>" />-
				<input type="text" maxlength="4" size="1" name="code4" id="code4"
				value="<?php if(isset($hash_champ_valeur['code4'])){echo $hash_champ_valeur['code4']; } ?>" />-
				<input type="text" maxlength="3" size="1" name="code5" id="code5"
				value="<?php if(isset($hash_champ_valeur['code5'])){echo $hash_champ_valeur['code5']; } ?>" />
			</td>
	
	</tr>

		<?php
		
		if ($_SESSION ['acces_clinique'] >= 1) {
			?><tr>
		<td></td>
		<td>By diagnosis form number :</td>
		<td><input type="text" style="width: 100px;" name="form" /></td>
	</tr><?php
			?><tr>
		<td></td>
		<td>By NIP number :</td>
		<td><input type="text" style="width: 100px;" name="activite" /></td>
		<td><input type='submit' value='submit' /></td>
	</tr><?php
		} else {
			?><tr>
		<td></td>
		<td><noncliquable>View form number : <input type="text"
				style="width: 100px;" name="form" readonly /></noncliquable></td>
	</tr><?php
			?><tr>
		<td></td>
		<td><noncliquable>NIP number : <input type="text"
				style="width: 100px;" name="form" readonly /></noncliquable></td>
	</tr><?php
		}
		?>
		</form>

	<tr>
		<td colspan="4"><br />
			<hr width="1000"> <br /></td>
	</tr>
	<tr>
		<td><b> All datas : </b></td>
		<td><a
			href="../commun/see_clinique.php?from=export&amp;ind=*&amp;view=diagnosis">view
				all diagnosis</a> <br /> <a
			href="../commun/see_clinique.php?from=export&amp;ind=*&amp;view=activity">view
				all activity records</a><br /> <br /> <a
			href="../commun/genere_fichierxls.php?export_type=diagnosis"><img
				src="./images/Excel.png" height='25'> download diagnosis spreadsheet
		</a><br /> <a
			href="../commun/genere_fichierxls.php?export_type=activity_form"><img
				src="./images/Excel.png" height='25'> download activity form
				spreadsheet </a></td>
	</tr>
</table>


<?php
	}
}

include ($_SESSION ['pathway'] . "/footer.php");

?>
